package pl.ydp.ivy.flex.ide.handlers;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import pl.ydp.ivy.IIvyProvider;
import pl.ydp.ivy.Retriever;
import pl.ydp.ivy.flex.ide.Activator;
import pl.ydp.ivy.flex.ide.ProjectIvyFilter;
import pl.ydp.ivy.flex.ide.WorkspaceIvyProjects;

import com.adobe.flexbuilder.project.ClassPathEntryFactory;
import com.adobe.flexbuilder.project.IClassPathEntry;
import com.adobe.flexbuilder.project.IFlexProject;
import com.adobe.flexbuilder.project.actionscript.ActionScriptCore;
import com.adobe.flexbuilder.project.actionscript.IActionScriptProject;
import com.adobe.flexbuilder.project.actionscript.IMutableActionScriptProjectSettings;

public class RetrieveProjectJob extends Job {
	
	protected IProject project;
	WorkspaceIvyProjects ivyProjects;
	
	public RetrieveProjectJob(String name, IProject project) {
		this( name, project, null );
	}

	public RetrieveProjectJob(String name, IProject project, WorkspaceIvyProjects ivyProjects) {
		super(name);
		
		this.ivyProjects	= ivyProjects;
		this.project		= project;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		if ( ActionScriptCore.getProject( project)==null )
			return Status.OK_STATUS;
		
		if ( ivyProjects==null )
			ivyProjects = new WorkspaceIvyProjects( project.getWorkspace().getRoot() );
		IIvyProvider ivyProvider = Activator.getDefault().getIvyProvider().getProjectIvyProvider(project);
		
		if ( ivyProvider.isIvyPresent() ) {
			try {
				ProjectIvyFilter filter = new ProjectIvyFilter( project, ivyProjects );

				ivyProvider.getIvy().getSettings().setCheckUpToDate( false );
				Retriever r = new Retriever( ivyProvider, monitor );
				r.setRetrieveMonitor( filter );
				r.retreieve();
				
				filter.doFilter( monitor );
				configureProject( ivyProvider, monitor );
			} catch (Exception e) {
				return new Status(Status.ERROR,Activator.PLUGIN_ID,e.getMessage(), e);
			}
		}
		
		return Status.OK_STATUS;
	}
	
	protected void configureProject( IIvyProvider ivyProvider, IProgressMonitor monitor ) throws CoreException {
		IActionScriptProject asProject = ActionScriptCore.getProject( project);
		
		IMutableActionScriptProjectSettings settings = asProject.getProjectSettingsClone();
		IClassPathEntry [] libEntries = settings.getLibraryPath();
		boolean pathConfigured = false;
		for( IClassPathEntry entry: libEntries ) {
			if ( entry.getKind()==IClassPathEntry.KIND_PATH ) {
				IPath path = entry.getLinkablePath();
				if ( path.lastSegment().equals( ivyProvider.getArtifactBaseFolder() ) ) {
					pathConfigured = true;
					break;
				}
			}
		}
		
		IFolder ivyLibDir = project.getFolder( ivyProvider.getArtifactBaseFolder() );
		if ( !ivyLibDir.exists() )
			ivyLibDir.create( true, true, monitor );
		
		if ( !pathConfigured ) {
			IClassPathEntry ivyEntry = ClassPathEntryFactory.newEntry( IClassPathEntry.KIND_PATH, "IVY", settings );
			if ( asProject instanceof IFlexProject )
				ivyEntry.setLinkType( IClassPathEntry.LINK_TYPE_INTERNAL );
			else
				ivyEntry.setLinkType( IClassPathEntry.LINK_TYPE_EXTERNAL );
			ivyEntry.setValue( ivyProvider.getArtifactBaseFolder() );
			

			IClassPathEntry [] nLibEntries = new IClassPathEntry[libEntries.length+1];
			System.arraycopy( libEntries, 0, nLibEntries, 0, libEntries.length );
			nLibEntries[nLibEntries.length-1] = ivyEntry;
			libEntries = nLibEntries;

			settings.setLibraryPath( libEntries );
			asProject.setProjectDescription( settings, monitor );
		}
		
		new RefreshProjectJob( "Refresh project", project ).schedule();
		//project.refreshLocal( IResource.DEPTH_INFINITE, monitor );
	}

}
